﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;

namespace LocationProvider
{
    public partial class Step1 : PortalModuleBase
    {
        private void BindData()
        {
            CWCareProviderDataContext _context = new CWCareProviderDataContext();
            var queryCity = (from loc in _context.cwc_Locations
                             where loc.city != null && loc.city != ""
                             select new { City = loc.city }).Distinct();

            var queryCounty = (from loc in _context.cwc_Locations
                               where loc.county != null && loc.county != ""
                               select new { County = loc.county }).Distinct();

            var queryProvider = from pro in _context.cwc_Providers
                                where pro.fName != null && pro.fName != "" && pro.lName != null && pro.lName != ""
                                select new { ID = pro.providerID, Name = pro.fName + ", " + pro.lName, LastName = pro.lName };

            var querySpecialty = from spe in _context.cwc_Specialties
                                 select spe;

            var queryLanguage = from lang in _context.cwc_SpokenLanguages
                                select lang;

            var queryHospital = from hos in _context.cwc_Hospitals
                                select hos;

            cboID.DataSource = queryProvider.OrderBy(p => p.LastName);
            cboID.DataTextField = "Name";
            cboID.DataValueField = "ID";
            cboID.DataBind();
            cboID.Items.Insert(0, new ListItem { Selected = true, Value = "Any", Text = "Any" });

            cboCity.DataSource = queryCity.OrderBy(p => p.City);
            cboCity.DataTextField = "City";
            cboCity.DataValueField = "City";
            cboCity.DataBind();
            cboCity.Items.Insert(0, new ListItem { Selected = true, Value = "Any", Text = "Any" });

            cboCounty.DataSource = queryCounty.OrderBy(p => p.County);
            cboCounty.DataTextField = "County";
            cboCounty.DataValueField = "County";
            cboCounty.DataBind();
            cboCounty.Items.Insert(0, new ListItem { Selected = true, Value = "Any", Text = "Any" });

            cboSpecialty.DataSource = querySpecialty.OrderBy(p => p.specialty);
            cboSpecialty.DataTextField = "specialty";
            cboSpecialty.DataValueField = "specialtyID";
            cboSpecialty.DataBind();
            cboSpecialty.Items.Insert(0, new ListItem { Selected = true, Value = "Any", Text = "Any" });

            cboLanguage.DataSource = queryLanguage;
            cboLanguage.DataTextField = "spokenLanguage";
            cboLanguage.DataValueField = "languageID";
            cboLanguage.DataBind();
            cboLanguage.Items.Insert(0, new ListItem { Selected = true, Value = "Any", Text = "Any" });

            cboHospital.DataSource = queryHospital;
            cboHospital.DataTextField = "hospitalName";
            cboHospital.DataValueField = "hospitalID";
            cboHospital.DataBind();
            cboHospital.Items.Insert(0, new ListItem { Selected = true, Value = "Any", Text = "Any" });
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //Passing search values via session
            SearchCriteria sc = new SearchCriteria();
            sc.ProviderID = cboID.SelectedValue;
            sc.City = cboCity.SelectedValue;
            sc.County = cboCounty.SelectedValue;
            sc.Language = cboLanguage.SelectedValue;
            sc.Specialty = cboSpecialty.SelectedValue;
            sc.Hospital = cboHospital.SelectedValue;
            Session.Add("CWCSearchCriteria", sc);

            DotNetNuke.Entities.Modules.PortalModuleBase objmodule = (PortalModuleBase)LoadControl("Step2.ascx");
            objmodule.ModuleConfiguration = this.ModuleConfiguration;            

            this.Controls.Clear();
            this.Controls.Add(objmodule);
        }
    }

    public class SearchCriteria
    {
        public string ProviderID { get; set; }
        public string City { get; set; }
        public string County { get; set; }
        public string Specialty { get; set; }
        public string Language { get; set; }
        public string Hospital { get; set; }
    }
}